package com.example.toutiao.ui.main.frg

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.jzvd.Jzvd
import com.example.toutiao.R
import com.example.toutiao.base.BaseFragment
import com.example.toutiao.ui.main.adpater.VideoAdapter
import com.example.toutiao.ui.main.adpater.VideoTabAdapter
import com.example.toutiao.ui.main.model.TabIdsModel
import com.example.toutiao.ui.main.presenter.VideoPresenter
import com.example.toutiao.ui.main.view.VideoView
import com.example.toutiao.ui.user.EvtMsgModel
import kotlinx.android.synthetic.main.fragment_video.view.*

class VideoFragment : BaseFragment<VideoView, VideoPresenter>(), VideoView {
    private var titles: MutableList<TabIdsModel>? = mutableListOf<TabIdsModel>()
    private var fragments: MutableList<Fragment> = mutableListOf<Fragment>()
    private var videoAdapter: VideoTabAdapter? = null
    private var views: View? = null
    override fun getLayoutId(): Int = R.layout.fragment_video

    override fun initView(view: View) {
        views = view
    }
    override fun initData() {
        getPresenter()!!.getTabIds()
    }

    override fun createPresenter(): VideoPresenter = VideoPresenter()

    override fun isRegister(): Boolean = true

    override fun <T> setData(data: T) {
        titles = data as MutableList<TabIdsModel>
        for (i: Int in titles!!.indices) {
            fragments.add(VideoListFragment(titles!![i].id))
        }
        if (videoAdapter == null) {
            videoAdapter = VideoTabAdapter(childFragmentManager, fragments, titles!!)
        } else {
            videoAdapter!!.notifyDataSetChanged()
        }
        views!!.vp_video.offscreenPageLimit = fragments.size
        views!!.vp_video.adapter = videoAdapter
        /* 监听页面变化 使用最新的 */
        views!!.vp_video.addOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                Jzvd.releaseAllVideos()
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
        views!!.stl_tab_title.setViewPager(views!!.vp_video)
    }

    override fun setError(err: String) {

    }

    override fun onDestroy() {
        super.onDestroy()
        views!!.vp_video.removeOnPageChangeListener(object:ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {

            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })
    }

    override fun getMessage(message: EvtMsgModel<*>) {
        super.getMessage(message)
        if(message.code==401){
            views!!.rec_title.text="${message.msg}"
        }
    }


}